﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;

using EFERP;
using HRM.DICTIONARY.DAL;

namespace HRM.DICTIONARY.UI
{
    public enum LoadOption
    {
        LoadPermissionByGroupID,
        LoadPermissionByUserID
    }
    public partial class xfrmPermission : DevExpress.XtraEditors.XtraForm
    {
        ICommon<sys_group> exeSysGroup;
        ICommon<sys_user> exeSysUser;
        ICommon<sys_user_rule> exeSysUserRule;
        ICommon<sys_object> exeSysObject;
        LoadOption option;

        public xfrmPermission()
        {
            InitializeComponent();
        }

        public void ReloadSystemUser()
        {
            LoadTreeListSystemUser();
        }
        public void ReloadSystemPermission(string UserID, string GroupID, LoadOption option)
        {
            LoadTreeListSystemPermission(UserID, GroupID, option);
        }
        private void LoadTreeListSystemUser()
        {
            try
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("ID");
                dt.Columns.Add("Name");
                dt.Columns.Add("ParentID");
                dt.Columns.Add("ImageIndex");

                List<sys_group> listGroup = exeSysGroup.GetList();

                foreach (sys_group obj in listGroup)
                {
                    if (obj.group_id.ToString() != "employee")
                    {
                        dt.Rows.Add(new object[] { obj.group_id.ToString(), obj.group_name.ToString(), "", 0 });
                        List<sys_user> listUser = obj.sys_user.Where(n => n.group_id == obj.group_id).ToList(); 
                        foreach (sys_user objUser in listUser)
                        {
                            dt.Rows.Add(new object[] { objUser.userid.ToString(), objUser.username.ToString(), objUser.group_id.ToString(), 1 });
                        }
                    }
                }
                this.tlSysUser.DataSource = dt;
                this.tlSysUser.ExpandAll();
            }
            catch(Exception) 
            {
                XtraMessageBox.Show("Không thể tải dữ liệu.");
            }
        }
        private void LoadTreeListSystemPermission(string UserID, string GroupID, LoadOption option)
        {
            try
            {
                var list1 = (from o in exeSysUserRule.DbSet
                             where o.active == true && o.sys_object.active == true
                             select new
                             {
                                 object_id = o.object_id,
                                 object_name = o.sys_object.object_name,
                                 description = o.sys_object.description,
                                 parent_id = o.sys_object.parent_id,
                                 level = o.sys_object.level,
                                 order_id = o.sys_object.order_id,
                                 owner = o.sys_object.owner,
                                 active = o.active,
                                 goup_id = o.goup_id,
                                 rule_id = o.rule_id,
                                 allowadd = o.allowadd,
                                 allowdelete = o.allowdelete,
                                 allowedit = o.allowedit,
                                 allowaccess = o.allowaccess,
                                 allowprint = o.allowprint,
                                 allowexport = o.allowexport,
                                 allowimport = o.allowimport
                             }).OrderBy(o => o.order_id).ToList();

                if (list1 == null)
                {
                    XtraMessageBox.Show("Không thể tải dữ liệu phân quyền");
                    return;
                }
                switch (option)
                {

                    case LoadOption.LoadPermissionByGroupID:
                        var list2 = list1.Where(n => n.goup_id == GroupID).ToList();
                        tlPermision.DataSource = list2;
                        break;

                    case LoadOption.LoadPermissionByUserID:
                        List<sys_user> listSysUser = exeSysUser.DbSet.ToList();
                        string GroupIDFilter = listSysUser.Where(n => n.userid == UserID).ToList().SingleOrDefault().group_id;
                        var list3 = list1.Where(n => n.goup_id == GroupIDFilter).ToList();
                        tlPermision.DataSource = list3;
                        break;
                }
                tlPermision.ExpandAll();
            }
            catch
            {
            }
        }
        private void xfrmPermission_Load(object sender, EventArgs e)
        {
            exeSysGroup = new Common<sys_group>(StaticVariable.ERPEn);
            exeSysUserRule = new Common<sys_user_rule>(StaticVariable.ERPEn);
            exeSysObject = new Common<sys_object>(StaticVariable.ERPEn);
            exeSysUser = new Common<sys_user>(StaticVariable.ERPEn);
            option = LoadOption.LoadPermissionByGroupID;
            ReloadSystemUser();
            ReloadSystemPermission("", "admin", LoadOption.LoadPermissionByGroupID);
        }

        private void treeList1_GetSelectImage(object sender, DevExpress.XtraTreeList.GetSelectImageEventArgs e)
        {
            DataRowView dataRecordByNode = (DataRowView)this.tlSysUser.GetDataRecordByNode(e.Node);
            e.NodeImageIndex = int.Parse(dataRecordByNode["ImageIndex"].ToString());
        }


    }
}